<template>
  <el-dialog
    :title="title"
    :close-on-click-modal="false"
     v-dialogDrag
    :visible.sync="visible">
    <el-form :model="inputForm" size="small" ref="inputForm" :class="method==='view'?'readonly':''"  :disabled="method==='view'" @keyup.enter.native="doSubmit()"
             label-width="120px">
      <el-row  :gutter="15">
        <el-col :span="12">
            <el-form-item label="器材名称" prop="equipmentName"
                :rules="[
                  {required: true, message:'器材名称不能为空', trigger:'blur'}
                 ]">
              <el-input v-model="inputForm.equipmentName" placeholder="请填写器材名称"     ></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="单位" prop="unit"
                :rules="[
                  {required: true, message:'单位不能为空', trigger:'blur'}
                 ]">
              <el-input v-model="inputForm.unit" placeholder="请填写单位"     ></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="计划用途" prop="purpose"
                :rules="[
                 ]">
              <el-input v-model="inputForm.purpose" placeholder="请填写计划用途"     ></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="用途" prop="used"
                :rules="[
                 ]">
              <el-input v-model="inputForm.used" placeholder="请填写用途"     ></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="规格" prop="norms"
                :rules="[
                  {required: true, message:'规格不能为空', trigger:'blur'}
                 ]">
              <el-input v-model="inputForm.norms" placeholder="请填写规格"     ></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="商品编号" prop="goodsNumber"
                :rules="[
                  {required: true, message:'商品编号不能为空', trigger:'blur'}
                 ]">
              <el-input v-model="inputForm.goodsNumber" placeholder="请填写商品编号"     ></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="出库id" prop="checkoutid.id"
                :rules="[
                 ]">
              <el-input v-model="inputForm.checkoutid.id" placeholder="请填写出库id"     ></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="入库id" prop="warehousid.id"
                :rules="[
                 ]">
              <el-input v-model="inputForm.warehousid.id" placeholder="请填写入库id"     ></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="数量" prop="number"
                :rules="[
                 ]">
              <el-input v-model="inputForm.number" placeholder="请填写数量"     ></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="单价" prop="unitprice"
                :rules="[
                 ]">
              <el-input v-model="inputForm.unitprice" placeholder="请填写单价"     ></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="合计" prop="sum"
                :rules="[
                 ]">
              <el-input v-model="inputForm.sum" placeholder="请填写合计"     ></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="存储位置" prop="storagelocation "
                :rules="[
                 ]">
              <el-input v-model="inputForm.storagelocation " placeholder="请填写存储位置"     ></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="类别" prop="type"
                :rules="[
                 ]">
                <el-select v-model="inputForm.type" placeholder="请选择"  style="width: 100%;">
                          <el-option
                            v-for="item in $dictUtils.getDictList('equipment_type')"
                            :key="item.value"
                            :label="item.label"
                            :value="item.value">
                          </el-option>
                      </el-select>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="备注信息" prop="remarks"
                :rules="[
                 ]">
          <el-input type="textarea" v-model="inputForm.remarks" placeholder="请填写备注信息"     ></el-input>
           </el-form-item>
        </el-col>
        </el-row>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button size="small" v-if="method === 'add'" type="primary" @click="continueDoSubmit()">继续添加</el-button>
      <el-button size="small" @click="visible = false">关闭</el-button>
      <el-button size="small" v-if="method !== 'view'" type="primary" @click="doSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        title: '',
        method: '',
        visible: false,
        oldInputForm: '',
        inputForm: {
          id: '',
          equipmentName: '',
          unit: '',
          purpose: '',
          used: '',
          norms: '',
          goodsNumber: '',
          checkoutid: {
            id: ''
          },
          warehousid: {
            id: ''
          },
          number: '',
          unitprice: '',
          sum: '',
          storagelocation : '',
          type: '',
          remarks: ''
        }
      }
    },
    components: {
    },
    methods: {
      init (method, obj) {
        this.method = method
        if (method === 'add') {
          this.title = `新建器材表`
        } else if (method === 'edit') {
          this.title = '修改器材表'
        } else if (method === 'view') {
          this.title = '查看器材表'
        }
        this.visible = true
        this.$nextTick(() => {
          this.$refs.inputForm.resetFields()
          this.inputForm.id = ''
          this.oldInputForm = ''
          if (method === 'edit' || method === 'view') { // 修改或者查看
            this.oldInputForm = obj
            this.inputForm = JSON.parse(JSON.stringify(obj))
          }
        })
      },
   // 表单提交
      doSubmit () {
        this.$refs['inputForm'].validate((valid) => {
          if (valid) {
            this.$emit('addRow', this.oldInputForm, JSON.parse(JSON.stringify(this.inputForm)))
            this.visible = false
          }
        })
      },
      continueDoSubmit () {
        this.$refs['inputForm'].validate((valid) => {
          if (valid) {
            this.$emit('addRow', this.oldInputForm, JSON.parse(JSON.stringify(this.inputForm)))
            this.$refs['inputForm'].resetFields()
          }
        })
      }
    }
  }
</script>